import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 1.导入组件
import login from '../views/user/login.vue'
import layout from '../views/layout.vue'
import home from '../views/home/home.vue'
import qa from '../views/qa/qa.vue'
import movie from '../views/movie/movie.vue'
import user from '../views/user/user.vue'
import notfound from '../views/notfound/notfound.vue'
import edit from '@/views/user/edit.vue'
import myArticle from '@/views/home/myArticle.vue'

// 2.路由规则
const routes=[
  {
    name:'login',
    path:'/login',
    component:login
  },
  {
    name:'edit',
    path:'/edit',
    component:edit
  },
  {
    name:'myArticle',
    path:'/myArticle',
    component:myArticle
  },
  {
    name:'layout',
    path:'/',
    component:layout,
    children:[
      {
        name:'home',
        path:'/home',
        component:home
      },
      {
        name:'qa',
        path:'/qa',
        component:qa
      },
      {
        name:'movie',
        path:'/movie',
        component:movie
      },
      {
        name:'user',
        path:'/user',
        component:user,
        meta:{
          isLogin:true
        }
      },
      {
        path:"/",
        redirect:'/home'
      }
      
    ]
  },
  {
    name:'notfound',
    path:'/notfound',
    component:notfound
  },
  {
    path:'*',
    redirect:"/notfound"
  }
]


const originPush=VueRouter.prototype.push
VueRouter.prototype.push=function(location,resolve,reject){
  if(resolve||reject){
    return originPush.call(this,location,resolve,reject)
  }else{
    return originPush.call(this,location).catch(err=>{

    })
  }
}



// 3.创建路由规则
const router = new VueRouter({
  routes
})

import {getToken} from '@/utils/token.js'
import {Toast} from 'vant'

router.beforeEach((to, from, next) => {
  console.log(to);
  console.log(from);
  if(to.meta.isLogin&&!getToken()){
    Toast.fail('请先登录');
    next({path:"/login"})
  }else{
    next()
  }
})

export default router

